package util;

import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * Created by Administrator on 2018/11/23.
 */

public class JdbcUtil {
    private final static String USERNAME;
    private final static String PASSWORD;
    private final static String URL;
    private final static String DRIVER;

    static {
        Properties p = new Properties();
        try {
            p.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        USERNAME = p.getProperty("mysql.userName");
        PASSWORD = p.getProperty("mysql.passWord");
        URL = p.getProperty("mysql.url");
        DRIVER = p.getProperty("mysql.driver");

        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnertion() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
            //		手动抛出异常
            throw new RuntimeException(e.getMessage());
        }
        return conn;
    }
    public static void close(Connection conn, Statement ps,ResultSet resultSet){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
